/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：太平金科-基础组件封装层-SpringCloud组件封装-缺省实现
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taipingframework.cloud.web.servlet;

import com.taipingframework.cloud.web.advice.ServiceAspect;
import com.taipingframework.cloud.web.openfeign.FeignClientAdvice;
import com.taipingframework.cloud.web.openfeign.ServletFeignClientAdvice;
import com.taipingframework.cloud.web.properties.FeignHeaderProperty;
import com.taipingframework.cloud.web.properties.MicroServiceProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Servlet;

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@Import(ServiceAspect.class)
public class FeignServletAutoConfig {

    private FeignHeaderProperty feignHeaderProperty;

    @Autowired(required = false)
    public void setFeignHeaderProperty(FeignHeaderProperty feignHeaderProperty) {
        this.feignHeaderProperty = feignHeaderProperty;
    }

    /**
     * Servlet环境下
     * 默认实现FeignClient请求与响应两个阶段的回调
     */
    @Bean
    @ConditionalOnMissingBean
    public FeignClientAdvice servletFeignClientAdvice() {
        return new ServletFeignClientAdvice(feignHeaderProperty);
    }


    /**
     * 微服务相关配置
     */
    @Bean
    @ConfigurationProperties(prefix = "application.service")
    public MicroServiceProperty microServiceProperty() {
        return new MicroServiceProperty();
    }

}
